package xyz.yuzegod.megawalls.util;

import net.minecraft.server.v1_8_R3.Entity;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.event.entity.CreatureSpawnEvent;
import xyz.yuzegod.megawalls.game.team.TeamWither;
import xyz.yuzegod.megawalls.nms.*;

import java.lang.reflect.Field;
import java.util.Map;

public enum EntityTypes
{
    Wither("Wither", 64, (Class<? extends Entity>)TeamWither.class), 
    Wolf("Wolf", 95, (Class<? extends Entity>)ShamanWolf.class), 
    Blaze("Blaze", 61, (Class<? extends Entity>)CustomBlaze.class), 
    Creeper("Creeper", 50, (Class<? extends Entity>)CustomCreeper.class), 
    Fireball("Fireball", 12, (Class<? extends Entity>)CustomCannonball.class), 
    Bat("Bat", 65, (Class<? extends Entity>)CustomBat.class), 
    Pig("Pig", 90, (Class<? extends Entity>)CustomPig.class), 
    BoomSheep("BoomSheep", 91, (Class<? extends Entity>)BoomSheep.class), 
    Spider("Spider", 52, (Class<? extends Entity>)CustomSpider.class);
    
    private EntityTypes(final String name, final int id, final Class<? extends Entity> custom) {
        addToMaps(custom, name, id);
    }
    
    public static void spawnEntity(final Entity entity, final Location loc) {
        entity.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
        ((CraftWorld)loc.getWorld()).getHandle().addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM);
    }
    
    private static Object getPrivateField(final String fieldName, final Class clazz, final Object object) {
        Object o = null;
        try {
            final Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            o = field.get(object);
        }
        catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        catch (IllegalAccessException e2) {
            e2.printStackTrace();
        }
        return o;
    }
    
    private static void addToMaps(final Class clazz, final String name, final int id) {
        ((Map)getPrivateField("c", net.minecraft.server.v1_8_R3.EntityTypes.class, null)).put(name, clazz);
        ((Map)getPrivateField("d", net.minecraft.server.v1_8_R3.EntityTypes.class, null)).put(clazz, name);
        ((Map)getPrivateField("f", net.minecraft.server.v1_8_R3.EntityTypes.class, null)).put(clazz, id);
    }
}
